<template>
  <el-form class="login-form"
           status-icon
           :rules="loginRules"
           ref="loginForm"
           :model="loginForm"
           label-width="0">
    <el-form-item prop="tenantCode"
                  v-if="isShowTenantCode">
      <el-input size="small"
                autocomplete="on"
                @keyup.enter.native="handleLogin"
                v-model="loginForm.tenantCode"
                :placeholder="$t('login.tenantCode')">
        <i slot="prefix"
           class="icon-canshu"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="username">
      <el-input size="small"
                autocomplete="on"
                @keyup.enter.native="handleLogin"
                v-model="loginForm.username"
                :placeholder="$t('login.username')">
        <i slot="prefix"
           class="icon-yonghu"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input size="small"
                autocomplete="on"
                @keyup.enter.native="handleLogin"
                :type="passwordType"
                v-model="loginForm.password"
                :placeholder="$t('login.password')">
        <i class="el-icon-view el-input__icon"
           slot="suffix"
           @click="showPassword"></i>
        <i slot="prefix"
           class="icon-mima"></i>
      </el-input>
    </el-form-item>
    <el-form-item v-if="website.isCaptcha" prop="code">
      <el-row :span="24">
        <el-col :span="16">
          <el-input size="small"
                    @keyup.enter.native="handleLogin"
                    v-model="loginForm.code"
                    auto-complete="off"
                    :placeholder="$t('login.code')">
            <i slot="prefix" class="icon-yanzhengma"/>
          </el-input>
        </el-col>
        <el-col :span="8">
          <div class="login-code">
            <img :src="loginForm.image" class="login-code-img" @click="refreshCode"
            />
          </div>
        </el-col>
      </el-row>
    </el-form-item>
    <el-form-item>
      <el-button type="primary"
                 size="small"
                 @click.native.prevent="handleLogin"
                 class="login-submit">{{$t('login.submit')}}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {
  mapGetters
} from "vuex";

import {getCaptcha} from '@/api/user';

export default {
  name: "userlogin",
  data () {
    return {
      loginForm: {
        username: "root",
        tenantCode: "000000",
        password: "123456",
        //验证码的值
        code: "",
        //验证码的索引
        key: "",
        //预加载白色背景
        image: "",
      },
      checked: false,
      loginRules: {
        username: [{
          required: true,
          message: "请输入用户名",
          trigger: "blur"
        }],
        tenantCode: [{
          required: true,
          message: "请输入租户编码",
          trigger: "blur"
        }],
        password: [{
          required: true,
          message: "请输入密码",
          trigger: "blur"
        }],
        code: [{
          required: true,
          message: "请输入验证码",
          trigger: "blur"
        }]
      },
      passwordType: "password",
      isShowTenantCode: false
    };
  },
  created () {
    this.refreshCode();
  },
  mounted () {

    if (this.website.isTenant){
      this.$store
          .dispatch("GetTenantCode").then((res) => {
        this.$store.commit("SET_LOGO", res.logo);
        if (res.tenantCode) {
          this.loginForm.tenantCode = res.tenantCode;
          this.isShowTenantCode = false
        } else {
          this.isShowTenantCode = true
        }
      });
    }

  },

  computed: {
    ...mapGetters(["tagWel", "website"])
  },
  props: [],
  methods: {
    refreshCode() {
      getCaptcha().then(res => {
        this.loginForm.key = res.data.data.key;
        this.loginForm.image = res.data.data.image;
      })
    },
    showPassword () {
      this.passwordType == "" ?
        (this.passwordType = "password") :
        (this.passwordType = "");
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {

          const loading = this.$loading({
            lock: true,
            text: '登录中,请稍后。。。',
            spinner: "el-icon-loading"
          });

          this.$store
            .dispatch("LoginByUsername", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.tagWel.value
              });
              loading.close();
            })
            .catch(err => {
              loading.close();
              console.log(err)
            });
        }
      });
    }
  }
};
</script>

<style>
</style>
